﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PRET.AttendanceReport.Forms;

namespace PRET.AttendanceReport
{
    public partial class MainForm : Form
    {
        delegate void CloseFromCallback();
        private string selectedReportType = "";
        private WaitForm waitform;
        private BackgroundWorker backgroundWait;
        private Control currentView = null;
        private static Dictionary<string, string> DicReportView = new Dictionary<string, string>{
            {"employeAtt", "人员考勤统计表"},
            {"teamAtt", "班组考勤汇总表"},
            {"teamAttEditor", "班组考勤修正时间表"}
        };

        public MainForm()
        {
            ShowInitializeWindow();
            InitializeComponent();
            // 关闭初始化窗口
            CloseInitializeWindow();
        }

        private void ShowInitializeWindow()
        {
            // 显示等待窗口
            this.waitform = new WaitForm();
            backgroundWait = new BackgroundWorker();
            this.backgroundWait.DoWork += showWaitForm;
            this.backgroundWait.RunWorkerAsync(this.waitform);
        }
        // 显示等待窗口
        private void showWaitForm(object sender, DoWorkEventArgs e)
        {
            ((WaitForm)e.Argument).ShowDialog();
        }
        // 关闭等待窗口
        private void CloseInitializeWindow()
        {
            if (this.waitform.InvokeRequired)
            {
                CloseFromCallback d = new CloseFromCallback(CloseInitializeWindow);
                this.waitform.Invoke(d);
            }
            else
            {
                this.waitform.CanClose = true;
                this.waitform.Close();
            }
        }

        #region 事件
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.reportSelectionCombox.DataSource = new BindingSource(DicReportView, null);
            this.reportSelectionCombox.ValueMember = "Key";
            this.reportSelectionCombox.DisplayMember = "Value";
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("是否确定退出程序？", "确认", MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
        }
        #endregion

        private void reportSelectionCombox_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<string, string> selectItem = (KeyValuePair<string, string>)this.reportSelectionCombox.SelectedItem;
            if (this.selectedReportType == selectItem.Key)
                return;
            else
            {
                if (currentView != null)
                    this.reportViewGroup.Controls.Remove(currentView);

                this.reportViewGroup.Text = selectItem.Value;
                this.selectedReportType = selectItem.Key;
                switch (this.selectedReportType)
                {
                    case "employeAtt":
                        this.reportViewGroup.Controls.Add(this.employeAttReportView);
                        currentView = this.employeAttReportView;
                        break;
                    case "teamAtt":
                        this.reportViewGroup.Controls.Add(this.teamAttReportView);
                        currentView = this.teamAttReportView;
                        break;
                    case "teamAttEditor":
                        this.reportViewGroup.Controls.Add(this.teamEditorReportView);
                        currentView = this.teamEditorReportView;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
